#include "message.h"
#include "net.h"
#include "dispather.h"
void onRpcquest(const rpc::BaseConnection::ptr &conn, const rpc::BaseMessage::ptr &msg)
{
    std::cout << "收到rpc请求" << std::endl;
    std::string body = msg->serialize();
    std::cout << body << std::endl;
    auto rpc_resp = rpc::MessageFactory::create<rpc::RpcResponse>();
    rpc_resp->setrId("11111");
    rpc_resp->setMtype(lxprpc::MType::RSP_RPC);
    rpc_resp->SetRCode(lxprpc::RCode::RCODE_OK);
    rpc_resp->Setresult("33");
    conn->send(rpc_resp);
}

void onTopicquest(const rpc::BaseConnection::ptr &conn, const rpc::BaseMessage::ptr &msg)
{
    std::cout << "收到topic请求" << std::endl;
    std::string body = msg->serialize();
    std::cout << body << std::endl;
    auto rpc_resp = rpc::MessageFactory::create<rpc::TopicResponse>();
    rpc_resp->setrId("2222");
    rpc_resp->setMtype(lxprpc::MType::RSP_TOPIC);
    rpc_resp->SetRCode(lxprpc::RCode::RCODE_OK);
    conn->send(rpc_resp);
}
int main()
{
    auto dispather = std::make_shared<rpc::dispather>();
    dispather->registerHandler(lxprpc::MType::REQ_RPC, onRpcquest);
    dispather->registerHandler(lxprpc::MType::REQ_TOPIC, onTopicquest);

    auto server = rpc::ServerFactory::create(9090);
    auto message_cb = std::bind(&rpc::dispather::onMessage, dispather.get(), std::placeholders::_1, std::placeholders::_2);
    server->setMessageCallback(message_cb);
    server->start();
    return 0;
}
